﻿using System;
using System.ComponentModel;
using System.IO;
using FileSquirrel.Logic.XSD;

namespace FileSquirrel.Logic
{
    public class Copier
    {
        private LibraryDataSet RequestFile;
        private Indexer ind;

        public Copier(string requestFile, string libname, string libdir)
        {
            ind = new Indexer(libname, libdir);

            RequestFile = new LibraryDataSet();
            RequestFile.ReadXml(requestFile);
            ind.RequestFilesName = Path.GetFileNameWithoutExtension(requestFile);
            Reader.GlobalRequestLibrary = RequestFile;
        }

        public void Copy(string copyToFilePath, BackgroundWorker bgw)
        {
            int iRFileCount = RequestFile.RequestList.Rows.Count;
            for (int i = 0; i < RequestFile.RequestList.Rows.Count; i++)
            {
                var row = (LibraryDataSet.RequestListRow)RequestFile.RequestList.Rows[i];
                if (File.Exists(row.Path))
                {
                    try
                    {
                        File.Copy(row.Path, Path.Combine(copyToFilePath, Path.GetFileName(row.Path)), true);
                        RequestFile.RequestList.Rows.Remove(RequestFile.RequestList.Rows[i]);
                        i--;
                        bgw.ReportProgress(iRFileCount);
                    }
                    catch
                    {
                    }
                }
            }

            ind.SaveRequest();
        }
    }
}